package com.startery.queue.list;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.startery.ConnectionFactory;
import com.startery.vo.Man;


public class BlockListQueueTest {
	

	public static void main(String[] args) {
		RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();

        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        //使用StringRedisSerializer来序列化和反序列化redis的ke
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());

        //开启事务
        redisTemplate.setEnableTransactionSupport(true);
        redisTemplate.setConnectionFactory(ConnectionFactory.getInstance());
        redisTemplate.afterPropertiesSet();
        
        //与某一个key进行绑定
        BoundListOperations<Object, Object> boundListOperations = redisTemplate.boundListOps("queue:list"); 
        Man man =new Man("serler","11");
        boundListOperations.rightPush(man);
        Object value1 = redisTemplate.opsForList().rightPop("queue:list");  
        System.out.println(1111);
        /**
                         * 实现命令 : BLPOP key
         * (阻塞式)弹出最左端的元素，如果 key 中没有元素，将一直等待直到有元素或超时为止
         *
         * @param key
         * @param timeout 等待的时间，单位秒
         * @return 弹出的元素
         */
        System.out.println("开始阻塞"+new Date());
        boundListOperations.rightPop(10, TimeUnit.MINUTES);
        System.out.println("结束阻塞"+new Date());

	}
	
	

}
